import type { RouteLocationNormalized, RouteRecordNormalized } from "vue-router";

export const getRawRoute = (route: RouteLocationNormalized): RouteLocationNormalized => {
    if (!route) return route;
    const { matched, ...opt } = route;
    return {
        ...opt,
        matched: (matched
            ? matched.map((item) => ({
                meta: item.meta,
                name: item.name,
                path: item.path
            }))
            : undefined) as RouteRecordNormalized[]
    };
};

export const pathResolve = (parentPath: string, path: string) => {
    if (isUrl(path)) return path;
    const childPath = path.startsWith('/') || !path ? path : `/${path}`;
    return `${parentPath}${childPath}`.replace(/\/\//g, '/').trim();
};

export const isUrl = (path: string): boolean => {
    const reg =
        /(((^https?:(?:\/\/)?)(?:[-:&=+$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&%@.\w_]*)#?(?:[\w]*))?)$/;
    return reg.test(path);
};
